<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域开始 -->
      <div class="avatar_box">
        <img src="../assets/logo.png">
      </div>
      <!-- 头像区域结束 -->

      <!-- 登录表单区域开始 -->
      <el-form class="login-from" :model="loginForm" :rules="loginRules" ref="refFrom">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 登录表单区域结束 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 页面加载触发
  created() {},
  // 方法
  methods: {
    // 点击重置清空用户名跟密码
    reset() {
      // this.$refs访问子组件清空数据
      this.$refs.refFrom.resetFields()
    },
    // 点击登录
    login() {
      // 表单预验证
      this.$refs.refFrom.validate(async valid => {
        // 返回的是priomes对象必须用async包裹 并判断有没验证成功
        if (!valid) return this.$message.error('用户名或密码错误')
        // 发送登录请求路径跟输入的账号密码 并结构赋值出来
        const { data: res } = await this.$http.post('login', this.loginForm)
        // 判断状态码是不是成功了 并弹出提示框
        if (res.meta.status !== 200) return this.$message.error('用户登录失败')
        this.$message.success('用户登录成功')
        // 调用api把token存入sessionStorage
        window.sessionStorage.setItem('token', res.data.token)
        // 编程式导航跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 50%;

    img {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-from {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
